package com.service.impl;

import com.dao.CommentDao;

import com.dto.BaseDto;
import com.dto.dataenum.MessageEnum;

import com.entity.Comment;


import com.page.PageInfo;
import com.service.CommentService;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;


@Service
public class CommentServiceImpl implements CommentService {
    @Resource
    CommentDao commentDao;

    public BaseDto getCommentByCmpid(Integer cmpid)throws Exception{
        List<Comment> comments = commentDao.getCommentsByCmpid(cmpid);
        MessageEnum messageEnum = comments.size()>0?MessageEnum.GETMEMBERSBYPAGESUCCESS:MessageEnum.GETMEMBERSBYPAGEFAIl;
        BaseDto dto = new BaseDto(messageEnum);//将枚举的结果放入dto构造
        dto.settList(comments);//将查询结果也放入dto
        return dto;
    }


    //表示首先到 缓存管理器  myCache 找有没有数据，根据cacheId
    @Cacheable(value = "myCache",key = "#cacheId")
    public BaseDto getCommentsByPage(PageInfo pageInfo, String cacheId){
        //枚举的具体值由三元表达式决定
        List<Comment> comments = commentDao.getCommentsByPage(pageInfo);
        MessageEnum messageEnum = comments.size()>0?MessageEnum.GETMEMBERSBYPAGESUCCESS:MessageEnum.GETMEMBERSBYPAGEFAIl;
        BaseDto dto = new BaseDto(messageEnum);//将枚举的结果放入dto构造
        dto.settList(comments);//将查询结果也放入dto
        return dto;
    }


    public BaseDto getCommentByCmid(Integer cmid)throws Exception{

        Comment comment = commentDao.getCommentByCmid(cmid);
        MessageEnum messageEnum = Optional.ofNullable(comment).isPresent()?MessageEnum.GETMEMBERBYMNOSUCCESS:MessageEnum.GETMEMBERBYMNOFAIL;
        BaseDto dto = new BaseDto(messageEnum);//将枚举的结果放入dto构造
        dto.setT(comment);//单个对象放入dto
        return dto;
    }





    public BaseDto add(Comment comment) throws Exception{
        Integer result = commentDao.add(comment);
        MessageEnum messageEnum = result>0?MessageEnum.ADDMEMBERSUCCESS:MessageEnum.ADDMEBERFAIl;
        BaseDto dto = new BaseDto(messageEnum);//将枚举的结果放入dto构造
        dto.setT(comment);
        return dto;
    }

    public BaseDto update(Comment comment) throws Exception{
        Integer result = commentDao.update(comment);
        MessageEnum messageEnum = result>0?MessageEnum.UPDATEMEMBERSUCCESS:MessageEnum.UPDATEMEBERFAIl;
        BaseDto dto = new BaseDto(messageEnum);//将枚举的结果放入dto构造
        return dto;
    }


}
